var kd = {
    device_number: "",
    url_encode: function (str) {
        str = (str + '').toString();
        return encodeURIComponent(str)
            .replace(/!/g, '%21')
            .replace(/'/g, '%27')
            .replace(/\(/g, '%28')
            .replace(/\)/g, '%29')
            //.replace(/\*/g, '%2A')
            .replace(/%20/g, '+');
    },
    create_sign: function (data) {
        var keys = _.sortBy(_.keys(data));
        var d = "";
        var that = this;
        var bw_key = $("#app_secret").val();

        console.log("dev_key=", bw_key);
        _.map(keys, function (key) {
            if (key == _.last(keys)) {
                d += key + "=" + that.url_encode(data[key]) + bw_key;
            } else {
                d += key + "=" + that.url_encode(data[key]) + "&"
            }
        });
        var sign = hex_sha512(d);
        console.log(d);
        console.log(sign);
        return sign;
    },
    bw_api: function (url, data) {
        data["timestamps"] = Date.now();
        data["app_id"] = $("#app_id").val();
        var base_url = $("#base_url").val();
        // console.log("data==", data);
        var sign = this.create_sign(data);
        // console.log("sign=", sign);
        var api_url = base_url + url + "?sign=" + sign;
        try {
            var a = $.ajax({
                type: "POST",
                url: api_url,
                data: JSON.stringify(data),
                "headers": {
                    "Content-Type": "application/json"
                },
                async: false,
                dataType: "json",
                cache: false,
                success: function (res) {
                    a = res;
                },
                error: function (xhr, type) {
                }
            }).responseText;
            return JSON.parse(a);
        } catch (err) {
            console.log('-----------11111-----------', err)
            return JSON.stringify({"code": 10003})
        }
    },
    get_device_list: function () {
        var url = "/api/iot/device/list/";
        var data = {};
        var a = kd.bw_api(url, data);
        this.rander_device_list(a);
        $("#api_return").html(JSON.stringify(a));
    },
    rander_device_list: function (api_data) {
        var $from = $("#device_list").empty();
        var device_list = api_data.data;
        _.each(device_list, function (device) {
            var $div_device = "<div onclick='kd.get_device_box_list(" + device.device_number + ")'>" + device.device_number + "</div>";
            $from.append($div_device);
        });
    },
    get_device_box_list: function (dn) {
        var url = "/api/iot/device/box/list/";
        var data = {"device_number": dn};
        var a = kd.bw_api(url, data);
        this.rander_device_box_list(a);
        this.device_number = dn;
        $("#api_return").html(JSON.stringify(a));
    },
    rander_device_box_list: function (api_data) {
        var $row = $("#device_box_list").empty();
        var device_list = api_data.data;
        _.each(device_list, function (device) {
            var $from = $("<div style='padding: 2px;width: 800px;text-align: center'></div>");
            var $div_device = "<div style='display: inline-block;border:1px solid silver;width: 100px;' >" + device.box_name + "</div>";
            $from.append($div_device);
            var $div_device = "<div style='display: inline-block;border:1px solid silver;width: 100px;'>" + device.lock_address + "</div>";
            $from.append($div_device);
            var $div_device = "<div style='display: inline-block;border:1px solid silver;width: 100px;'>" + device.box_status + "</div>";
            $from.append($div_device);
            var $div_device = "<div style='display: inline-block;border:1px solid silver;width: 100px;'>" + device.enable_status + "</div>";
            $from.append($div_device);
            var $div_device = "<div style='display: inline-block;border:1px solid silver;width: 100px;'>" + device.box_size + "</div>";
            $from.append($div_device);
            var $div_device = "<div style='display: inline-block;border:1px solid silver;width: 100px;'><button onclick='kd.open_box(\"" + device.lock_address + "\")'>Open Box</button></div>";
            $from.append($div_device);
            $row.append($from);
        });
    },
    open_box: function (lock_address) {
        console.log("------------lock_address=", lock_address);
        var url = "/api/iot/open/box/";
        var data = {
            "device_number": this.device_number,
            "lock_address": lock_address,
            "use_type": "S"
        };
        var a = kd.bw_api(url, data);
        $("#api_return").html(JSON.stringify(a));
    },
    step1_set_save_order: function (dn, user_id, code, start_date, end_date) {
        var url = "/api/iot/v5/set/save/order/";
        var data = {"device_number": dn, "user_id": user_id, "code": code , "start_date": start_date, "end_date": end_date};
        var a = kd.bw_api(url, data);
        return a;
    },
    step2_update_order: function (dn, order_id, code, status, start_date, end_date) {
        var url = "/api/iot/v5/update/order/";
        var data = {"device_number": dn, "order_id": order_id, "code": code , "status": status, "start_date": start_date, "end_date": end_date};
        var a = kd.bw_api(url, data);
        return a;
    },
    step3_get_order: function (dn, user_id, code) {
        var url = "/api/iot/v5/get/order/";
        var data = {"device_number": dn, "user_id": user_id, "code": code};
        var a = kd.bw_api(url, data);
        return a;
    },
    step4_get_can_save_boxes: function (dn, box_size) {
        var url = "/api/iot/v5/get/can/save/box/";
        var data = {"device_number": dn, "box_size": box_size };
        var a = kd.bw_api(url, data);
        return a;
    },
    step4_get_device_boxes: function (dn, box_size) {
        var url = "/api/iot/device/box/list/";
        var data = {"device_number": dn};
        var a = kd.bw_api(url, data);
        return a;
    },
    step5_order_check_in: function (dn, order_id, box_id, action_date) {
        var url = "/api/iot/v5/order/check_in/";
        var data = {"device_number": dn, "order_id": order_id, "box_id": box_id, "action_date": action_date};
        var a = kd.bw_api(url, data);
        return a;
    },
    step6_order_in_progress: function (dn, order_id, action_date) {
        var url = "/api/iot/v5/order/in_progress/";
        var data = {"device_number": dn, "order_id": order_id, "action_date": action_date};
        var a = kd.bw_api(url, data);
        return a;
    },
    step7_order_check_out: function (dn, order_id, action_date) {
        var url = "/api/iot/v5/order/check_out/";
        var data = {"device_number": dn, "order_id": order_id, "action_date": action_date};
        var a = kd.bw_api(url, data);
        return a;
    },
    get_device_box_open_status: function (dn, lock_address) {
        var url = "/api/iot/device/box/status/";
        var data = {"device_number": dn, "lock_address": lock_address};
        var a = kd.bw_api(url, data);
        return a;
    },
}